import _ from "underscore"
var key = 'ZYKJ-Shop-Car'
function getShopCar(){
    var a = wx.getStorageSync(key)
    if(a){
        return JSON.parse(a)
    }
    return []
}
function setShopCar(shopCar){
    wx.setStorageSync(key, JSON.stringify(shopCar))
}
function removeShopCar(){
    var a = wx.removeStorageSync(key)
}
let cacheCar = getShopCar()
const shopCar = {
    state: {
        info: cacheCar
    },

    mutations: {
        SET_SHOP_CAR: (state, shopCar) => {
            state.info = shopCar
        },
        ADD_SHOP_CAR: (state, shopCar) => {
            var has = false
            state.info.forEach(v => {
                if(v.prdId == shopCar.prdId && JSON.stringify(v.buySpecs) == JSON.stringify(shopCar.buySpecs)){
                    v.count += shopCar.count
                    has = true
                }
            });
            if(!has){
                // shopCar.count = 1
                state.info.push(shopCar)
            }
        },
    },

    actions: {
        addShopCar({ commit, state }, shopCar) {
            var s = JSON.stringify(shopCar)
            commit('ADD_SHOP_CAR', JSON.parse(s))
            setShopCar(state.info)
        },
        setShopCar({ commit, state }, list) {
            commit('SET_SHOP_CAR', list)
            setShopCar(state.info)
        }
    }
};

export default shopCar;
